package com.scw.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.scw.model.SysUser;
import com.scw.service.UserService;
import com.scw.util.CommonNameUtil;
import com.scw.util.CommonZiDianMapUitl;
import com.scw.util.MD5Util;

/**
 * 用户操作类
 * @author 王华聪
 *
 */
public class UserAction {
	private SysUser sysUser;
	private UserService userService;
	private String vcode;//验证码
	private Map<String,String> zodiac;
	
	
	public Map<String, String> getZodiac() {
		return zodiac;
	}
	public void setZodiac(Map<String, String> zodiac) {
		this.zodiac = zodiac;
	}
	/**
	 * 注册用户
	 * @return 
	 */
	public String add(){
		HttpServletRequest req=ServletActionContext.getRequest();
		HttpSession session =req.getSession();
		if(vcode != null && !"".equals(vcode) && session.getAttribute("checkCode").toString().toLowerCase().equals(vcode.toLowerCase())){
			sysUser.setPassword(new MD5Util(sysUser.getPassword()).compute());
			userService.addUser(sysUser);
			session.setAttribute(CommonNameUtil.SessionUser, sysUser);
			//成功就跳到个人收藏夹
			return "main";
		}else{
			//不成功返回注册页面
			req.setAttribute("sysUser", sysUser);
			return "add";
		}
	}
	/**
	 * 个人资料
	 * @return
	 */
	public String edit(){
		HttpServletRequest req=ServletActionContext.getRequest();
		String dowhat=req.getParameter("dowhat");
		Long id=Long.valueOf(req.getParameter("id"));
		sysUser=userService.get(id);
		req.setAttribute("sysUser", sysUser);
		req.setAttribute("zodiac", CommonZiDianMapUitl.ZODIAC);
		if(dowhat!=null)
			return "editUser";
		return "edit";
	}
	/**
	 * 更新保存个人资料
	 * @return
	 */
	public String updat(){
		HttpServletRequest req=ServletActionContext.getRequest();
		String dowhat=req.getParameter("dowhat");
		if(sysUser.getPassword()!=null && !"".equals(sysUser.getPassword())){
			sysUser.setPassword(new MD5Util(sysUser.getPassword()).toString());
		}
		userService.updateUser(sysUser);
		if(dowhat!=null)
			return "list";
		return "ok";
	}
	
	/**
	 * 所有用户列表
	 * @return
	 */
	public String all(){
		HttpServletRequest req=ServletActionContext.getRequest();
		if(sysUser==null){
			sysUser=new SysUser();
		}
		List<SysUser> list = new ArrayList<SysUser>();
		list=userService.getAllUser(sysUser);
		req.setAttribute("list", list);
		req.setAttribute("sysUser", sysUser);
		return "all";
	}
	/**
	 * 删除用户
	 * @return
	 */
	public String del(){
		HttpServletRequest req=ServletActionContext.getRequest();
		String[] ids=(String[]) req.getAttribute("checkboxs");
		SysUser sysUser=new SysUser();
		for(String id:ids){
			sysUser.setId(Long.valueOf(id));
			userService.delUser(sysUser);
		}
		return "list";
	}

	public SysUser getSysUser() {
		return sysUser;
	}

	public void setSysUser(SysUser sysUser) {
		this.sysUser = sysUser;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getVcode() {
		return vcode;
	}

	public void setVcode(String vcode) {
		this.vcode = vcode;
	}

	

}
